(function (exports) {
    var FightHero = cc.Node.extend({
        ctor: function (para) {
            this._super();
            var that = this;

            var hero = new sp.SkeletonAnimation('res/image/spine/'+ para['name'] +'.json',
                'res/image/spine/'+ para['name'] +'.atlas');
            this.addChild(hero);
            this.getBloodPro(hero);
            this.getBluePro(hero);
            if(para['fanzhuan'])  hero.setScaleX(-1);

            this.hero = hero;
            this.heroname = para['name'];
            this.blood = para['blood'];
            this.bloodall = para['blood'];
            this.blue = para['blue'];
            this.blueall = 100;

        },
        getHero: function(){
            return this.hero;
        },
        playAnimation: function(trackIndex, name, loop, delay){
            this.hero.clearTracks();
            this.hero.addAnimation(trackIndex, name, loop, delay);
        },
        //血量
        getBlood: function(){
            return this.blood;
        },
        getBloodPro: function(hero){
            var bloodbg = new cc.Sprite('res/image/fight/hp_bg.png');
            bloodbg.setPosition(cc.p(0, 200));
            this.addChild(bloodbg);

            var bloodPro = new cc.ProgressTimer(cc.Sprite.create('res/image/fight/hp_green.png'));
            bloodPro.setPosition(cc.p(bloodbg.getContentSize().width / 2, bloodbg.getContentSize().height / 2));
            bloodPro.setType(cc.ProgressTimer.TYPE_BAR);
            bloodPro.setMidpoint(cc.p(0, 0));
            bloodPro.setBarChangeRate((hero.getScaleX() == 1)?cc.p(1, 0):cc.p(-1, 0));
            bloodbg.addChild(bloodPro);
            bloodPro.setPercentage(100);
            this.bloodPro = bloodPro;
        },
        bloodAni: function(num){
            this.blood += num;
            this.bloodPro.setPercentage(this.blood / this.bloodall * 100);
            var numtext = (num > 0) ? ('+' + num):num;
            var blood = new cc.LabelBMFont(numtext, 'res/image/fonts/jianxue.fnt');
            blood.setPosition(cc.p(0, 100));
            this.addChild(blood);
            blood.runAction(cc.sequence(
                cc.moveBy(0.5, cc.p(0, 200)),
                cc.delayTime(0.8)
                , cc.fadeOut(1)
                , cc.callFunc(function (sender) {
                    blood.removeFromParent();
                })
            ))
        },
        //魔法
        getBlue: function(){
            return this.blue;
        },
        getBluePro: function(hero){
            var bluebg = new cc.Sprite('res/image/fight/hp_bg.png');
            bluebg.setPosition(cc.p(0, 180));
            this.addChild(bluebg);

            var bluePro = new cc.ProgressTimer(cc.Sprite.create('res/image/fight/hp_green.png'));
            bluePro.setPosition(cc.p(bluebg.getContentSize().width / 2, bluebg.getContentSize().height / 2));
            bluePro.setType(cc.ProgressTimer.TYPE_BAR);
            bluePro.setMidpoint(cc.p(0, 0));
            bluePro.setBarChangeRate((hero.getScaleX() == 1)?cc.p(1, 0):cc.p(-1, 0));
            bluebg.addChild(bluePro);
            bluePro.setPercentage(0);
            this.bluePro = bluePro;

            var bluePro2 = new cc.ProgressTimer(cc.Sprite.create('res/image/fight/hp_yellow.png'));
            bluePro2.setPosition(cc.p(bluebg.getContentSize().width / 2, bluebg.getContentSize().height / 2));
            bluePro2.setType(cc.ProgressTimer.TYPE_BAR);
            bluePro2.setMidpoint(cc.p(0, 0));
            bluePro2.setBarChangeRate((hero.getScaleX() == 1)?cc.p(1, 0):cc.p(-1, 0));
            bluebg.addChild(bluePro2);
            bluePro.setPercentage(0);
            this.bluePro2 = bluePro2;
            bluePro2.setVisible(false);
        },
        blueAni: function(num){
            this.blue += num;
            var bl = this.blue / this.blueall * 100;
            if(bl >= 100){
                this.bluePro.setPercentage(100);
                this.bluePro2.setVisible(true);
                this.bluePro2.runAction(cc.sequence(
                    cc.progressTo(0.1, bl - 100)
                ));
            }else {
                this.bluePro2.setVisible(false);
                this.bluePro.runAction(cc.sequence(
                    cc.progressTo(0.1, bl)
                ));
            }
        },

        //skill
        biaoFly: function(src, pos, fromnode, tonode, func){
            var biao = new cc.Sprite('res/image/effect/' + src + '.png');
            biao.setPosition(pos);
            fromnode.addChild(biao);

            biao.runAction(cc.sequence(
                cc.moveBy(0.1, cc.p(tonode.getPositionX() - fromnode.getPositionX() - pos.x, 0)),
                cc.callFunc(function(){ func() }),
                cc.removeSelf()
            ));
            return biao;
        }
    });
    exports.FightHero = FightHero;
})(window);
